/**
 * 实现 bindActionCreators
 * @export
 * @param {object|function} actionCreator  传入一个对象返回一个对象，函数返回函数
 * @param {function} dispatch
 * @return {*} 
 */
export default function bindActionCreators(actionCreator, dispatch) {
    if (Object.prototype.toString.call(actionCreator) === '[object Object]') {
        const obj = {}
        for (const key in actionCreator) {
            if (Object.hasOwnProperty.call(actionCreator, key)) {
                const creator = actionCreator[key]
                if (typeof creator === 'function') { // 验证属性值   不是函数不处理
                    obj[key] = getAutoDispatchActionCreator(creator, dispatch)
                }
            }
        }
        return obj
    } else if (typeof actionCreator === 'function') {
        return getAutoDispatchActionCreator(actionCreator, dispatch)
    } else {
        throw new TypeError('actionCreator must be a function or Object')
    }
}

/**
 * 得到一个自动分发的action创建函数
 * @param {*} actionCreator
 * @param {*} dispatch
 * @return {*} 
 */
function getAutoDispatchActionCreator(actionCreator, dispatch) {
    return function (...args) {
        dispatch(actionCreator(...args))
    }
}